<template>
  <div :class="className" :style="{height: height, width: width}" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons'); // echarts theme

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '500px'
    },
    chartData: {
      type: Array,
      required: true
    },
    indicator: {
      type: String,
      default: 'ph'
    }
  },
  data() {
    return {
      chart: null,
      // 指标名称映射
      indicatorLabels: {
        ph: 'pH值',
        dissolvedOxygen: '溶解氧(mg/L)',
        cod: '化学需氧量(mg/L)',
        nh3n: '氨氮(mg/L)',
        totalPhosphorus: '总磷(mg/L)',
        totalNitrogen: '总氮(mg/L)'
      },
      // 指标对应的数据字段
      indicatorFields: {
        ph: 'avgPh',
        dissolvedOxygen: 'avgDissolvedOxygen',
        cod: 'avgCod',
        nh3n: 'avgNh3n',
        totalPhosphorus: 'avgTotalPhosphorus',
        totalNitrogen: 'avgTotalNitrogen'
      }
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      }
    },
    indicator() {
      this.setOptions(this.chartData);
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons');
      this.setOptions(this.chartData);
      window.addEventListener('resize', this.chart.resize);
    },
    setOptions(chartData) {
      if (!this.chart || !chartData || chartData.length === 0) {
        return;
      }
      
      // 获取当前选中的指标字段
      const indicatorField = this.indicatorFields[this.indicator];
      const indicatorLabel = this.indicatorLabels[this.indicator];
      
      // 准备数据
      const xAxisData = chartData.map(item => item.point_name);
      const seriesData = chartData.map(item => item[indicatorField]);
      
      // 设置不同指标的颜色
      let color = '#5470c6';
      switch(this.indicator) {
        case 'ph':
          color = '#5470c6';
          break;
        case 'dissolvedOxygen':
          color = '#91cc75';
          break;
        case 'cod':
          color = '#ee6666';
          break;
        case 'nh3n':
          color = '#fac858';
          break;
        case 'totalPhosphorus':
          color = '#73c0de';
          break;
        case 'totalNitrogen':
          color = '#9a60b4';
          break;
      }
      
      this.chart.setOption({
        title: {
          text: `不同水体${indicatorLabel}对比`,
          left: 'center'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          formatter: (params) => {
            const data = params[0];
            return `${data.name}<br/>${indicatorLabel}: ${data.value}`;
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: xAxisData,
            axisTick: {
              alignWithLabel: true
            },
            axisLabel: {
              rotate: 30,
              interval: 0
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: indicatorLabel,
            nameLocation: 'end',
            nameGap: 15,
            nameTextStyle: {
              fontWeight: 'bold'
            },
            axisLine: {
              show: true
            },
            axisTick: {
              show: true
            }
          }
        ],
        series: [
          {
            name: indicatorLabel,
            type: 'bar',
            barWidth: '60%',
            data: seriesData,
            itemStyle: {
              color: color
            },
            label: {
              show: true,
              position: 'top',
              formatter: '{c}'
            }
          }
        ]
      });
    }
  }
};
</script>